﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_SelectPeopleControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        DropDownList ddl区划;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            ddl区划 = (DropDownList)FindControl("ddl区划");//找到ddl区划这个下拉框
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                GetQuHua();
                BinderData();
            }
            Button bt_query = (Button)FindControl("bt_query");
            bt_query.Click += new EventHandler(bt_query_Click);
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Label lblHeader = (Label)FindControl("lblHeader");


        }

        //绑定行政区划下拉框
        public void GetQuHua()
        {
            string sql = @"select keyname from zs_params where name = '区属'";
            DataTable dt = _EluZSDataProvider.GetOneDs("(" + sql + ")", "*", "").Tables[0];
            ddl区划.DataValueField = "keyname";
            ddl区划.DataTextField = "keyname";
            ddl区划.DataSource = dt;
            ddl区划.DataBind();
            ddl区划.Items.Insert(0, "所有");

        }
        private void BinderData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            string sqlStr = string.Empty;

            string strQS = HttpContext.Current.Request["qs"];
            DataTable dt = _EluZSDataProvider.GetSelPeople(GetStrWhere());
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.HideColName = "ID";
            CustomGridView1.DataKeyNames = new string[] { "ID" };
            CustomGridView1.DataBind();
        }
        protected string GetStrWhere()
        {
            string sqlStr = "";

            if (ddl区划.SelectedValue != "所有")
            {
                sqlStr += " and pid in (select id from zs_prono where pid in (select id from zs_project where 区属 = '" + ddl区划.SelectedValue + "'))";
            }
            TextBox txt_name = (TextBox)FindControl("txt_name");

            if (!string.IsNullOrEmpty(txt_name.Text.Trim()))
            {
                sqlStr += " and pid in (select id from zs_prono where pid in (select id from zs_project where 项目名称 like '%" + txt_name.Text.Trim() + "%')) ";
            }
            TextBox 征收决定号 = (TextBox)FindControl("征收决定号");
            TextBox 房屋坐落 = (TextBox)FindControl("房屋坐落");
            TextBox 立项号 = (TextBox)FindControl("立项号");
            if (!string.IsNullOrEmpty(征收决定号.Text.Trim()))
            {

                sqlStr += " and pid in (select id from zs_prono where 征收许可证号 like '%" + ReturnString(征收决定号.Text.Trim()) + "%') ";
            }
            if (!string.IsNullOrEmpty(房屋坐落.Text.Trim()))
            {//(select 房屋坐落 from zs_zsperson where id = t.zspid)
                sqlStr += " and zspid in (select id from zs_zsperson where 房屋坐落 like '%" + 房屋坐落.Text.Trim() + "%') ";
            }
            if (!string.IsNullOrEmpty(立项号.Text.Trim()))
            {
                sqlStr += " and pid in (select id from zs_prono where pid in (select id from zs_project where 立项号 like '%" + 立项号.Text.Trim() + "%')) ";
            }
            TextBox 被征收人 = (TextBox)FindControl("被征收人");
            if (!string.IsNullOrEmpty(被征收人.Text.Trim()))
            {
                sqlStr += " and zspid in (select id from zs_zsperson where 产权人 like '%" + 被征收人.Text.Trim() + "%') ";
            }
            return sqlStr;
        }
        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            TableCell cell = new TableCell();

            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableHeaderCell cell1 = new TableHeaderCell();
                cell1.Text = "选择";
                e.Row.Cells.AddAt(0, cell1);
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rows = e.Row.DataItem as DataRowView;
                HtmlInputRadioButton rb = new HtmlInputRadioButton();

                if (rows != null)
                {
                    e.Row.Attributes.Add("id", rows["ID"].ToString());

                    rb.Attributes.Add("onclick", "setValue(this);");
                    rb.Attributes.Add("id", rows["ID"].ToString());
                }
                else
                {
                    e.Row.Attributes.Add("id", "");
                    rb.ID = "";
                    rb.Attributes.Add("onclick", "setValue(this);");
                }
                cell.Controls.Add(rb);
                e.Row.Cells.AddAt(0, cell);
            }

        }

        public string ReturnString(string txt)
        {
            string str = string.Empty;
            if (txt.Trim().Length == 7)
            {
                int i;
                if (int.TryParse(txt.Trim(), out i))
                {
                    string nian = txt.Trim().Substring(0, 4);
                    string yue = txt.Trim().Substring(4, 3);
                    str = nian + "年第" + yue;
                }
                else
                {
                    str = txt;
                }
            }
            else
            {
                str = txt;
            }
            return str;

        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BinderData();
        }
        protected void bt_query_Click(object sender, EventArgs e)
        {
            BinderData();
        }
    }
}


